/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Explorer;


import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.io.File; 

import javax.swing.Box; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.JTextField; 
import javax.swing.JTree; 
import javax.swing.event.TreeSelectionEvent; 
import javax.swing.event.TreeSelectionListener; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.tree.DefaultMutableTreeNode; 
import javax.swing.tree.DefaultTreeModel; 
import javax.swing.tree.TreePath; 

public class MyGUI extends JFrame implements TreeSelectionListener{ 
    private static final long serialVersionUID = 1L; 
    private DefaultMutableTreeNode root; 
    private DefaultTreeModel dtm; 
    private JTree myTree; 
    private JTable tblFile; 
    private DefaultTableModel dtmFile; 
    private JTextField tfAddress; 
     
    MyGUI() { 
        super("Explorer"); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
        createGUI(); 
        createTable(); 
        root.add(builtNode("D:\\")); 
    } 
    void createGUI() { 
        JPanel pTop=new JPanel(); 
        Box b=Box.createHorizontalBox(); 
        b.add(new JButton("Back")); 
        b.add(Box.createHorizontalStrut(5)); 
        b.add(new JButton("Forward")); 
        b.add(Box.createHorizontalStrut(5)); 
        b.add(new JButton("Up")); 
        b.add(Box.createHorizontalStrut(10)); 
        b.add(new JLabel("Address: ")); 
        b.add(Box.createHorizontalStrut(2)); 
        b.add(tfAddress=new JTextField(30)); 
        tfAddress.setText("My Computer"); 
        b.add(Box.createHorizontalStrut(2)); 
        b.add(new JButton("Go")); 
        pTop.add(b); 
        this.add(pTop, BorderLayout.NORTH); 
         
        root=new DefaultMutableTreeNode("My Computer"); 
        dtm=new DefaultTreeModel(root); 
        myTree=new JTree(dtm); 
        myTree.addTreeSelectionListener(this); 
        JScrollPane sp=new JScrollPane(myTree); 
        sp.setPreferredSize(new Dimension(250, 10)); 
        this.add(sp, BorderLayout.WEST); 
    } 
    void createTable() { 
        String []title={"Name","Size","Type"}; 
        tblFile=new JTable(dtmFile=new DefaultTableModel(title, 0)); 
        tblFile.getTableHeader().setFont(new Font("Arial", Font.BOLD, 12)); 
        this.add(new JScrollPane(tblFile), BorderLayout.CENTER); 
    } 
    public DefaultMutableTreeNode builtNode(String Drive) { 
        DefaultMutableTreeNode drive=new DefaultMutableTreeNode(Drive); 
        traverse(drive, Drive); 
        return drive; 
    } 
    void traverse(DefaultMutableTreeNode node, String Drive) { 
        try { 
            File f=new File(Drive); 
            for (File fn : f.listFiles()) { 
                if (fn.isDirectory()) { 
                    DefaultMutableTreeNode node2=new DefaultMutableTreeNode(fn.getName()); 
                    node.add(node2); 
                    traverse(node2, fn.getPath()); 
                } 
            } 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
    void getDetail(String path){ 
        for (int i=0; i<dtmFile.getRowCount(); ) 
            dtmFile.removeRow(0); 
        File f=new File(path); 
        for (File fn : f.listFiles()) 
            if (f.isDirectory()) { 
                String []row={fn.getName(), "", "File Folder"}; 
                dtmFile.insertRow(0, row); 
            } 
            else { 
                String []row={fn.getName(), fn.length() + " KB", ""}; 
                dtmFile.addRow(row); 
            } 
    } 
    public void valueChanged(TreeSelectionEvent e) { 
        if (myTree.getSelectionPath().getParentPath()!=null) { 
            TreePath path=e.getPath(); 
            Object []dd=path.getPath(); 
            String x=""; 
            for (Object o:dd) 
                x+=o.toString() + "\\"; 
            try { 
                x=x.substring(12, x.length()); 
                tfAddress.setText(x); 
                getDetail(x); 
            } catch (Exception e1) { 
                e1.printStackTrace(); 
            } 
        } 
    } 
    public static void main(String[] args) { 
        MyGUI f=new MyGUI(); 
        f.setVisible(true); 
        f.setSize(850, 600); 
    } 
}  
